package org.swqsbssddqs.shortlink.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.swqsbssddqs.shortlink.project.dao.entity.ShortLinkDO;
import org.swqsbssddqs.shortlink.project.dao.mapper.ShortLinkMapper;
import org.swqsbssddqs.shortlink.project.dto.req.RecycleBinRecoverReqDTO;
import org.swqsbssddqs.shortlink.project.dto.req.RecycleBinRemoveReqDTO;
import org.swqsbssddqs.shortlink.project.dto.req.RecycleBinSaveReqDTO;
import org.swqsbssddqs.shortlink.project.dto.req.ShortLinkRecycleBinPageReqDTO;
import org.swqsbssddqs.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import org.swqsbssddqs.shortlink.project.service.RecycleBinService;

import static org.swqsbssddqs.shortlink.project.common.constant.RedisCacheConstant.SHORT_LINK_REDIRECT_IS_NULL_KEY;
import static org.swqsbssddqs.shortlink.project.common.constant.RedisCacheConstant.SHORT_LINK_REDIRECT_KEY;

@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<ShortLinkMapper, ShortLinkDO> implements RecycleBinService {

    private final StringRedisTemplate stringRedisTemplate;

    public void saveRecycleBin(RecycleBinSaveReqDTO requestParam){
        LambdaUpdateWrapper<ShortLinkDO> eq = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getEnableStatus, 1)
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl());
        ShortLinkDO shortLinkDO = new ShortLinkDO().setEnableStatus(0);
        baseMapper.update(shortLinkDO,eq);
        stringRedisTemplate.delete(String.format(SHORT_LINK_REDIRECT_KEY,requestParam.getFullShortUrl()));
    }

    @Override
    public IPage<ShortLinkPageRespDTO> pageShortLink(ShortLinkRecycleBinPageReqDTO requestParam) {
        LambdaQueryWrapper<ShortLinkDO> eq = Wrappers.lambdaQuery(ShortLinkDO.class)
                .eq(ShortLinkDO::getDelFlag, 0)
                .in(ShortLinkDO::getGid, requestParam.getGidList())
                .eq(ShortLinkDO::getEnableStatus, 0);
        IPage<ShortLinkDO> res = baseMapper.selectPage(requestParam, eq);
        return res.convert(each-> BeanUtil.copyProperties(each,ShortLinkPageRespDTO.class));
    }

    @Override
    public void recoverRecycleBin(RecycleBinRecoverReqDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> eq = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getEnableStatus, 0)
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl());
        ShortLinkDO shortLinkDO = new ShortLinkDO().setEnableStatus(1);
        baseMapper.update(shortLinkDO,eq);
        stringRedisTemplate.delete(String.format(SHORT_LINK_REDIRECT_IS_NULL_KEY,requestParam.getFullShortUrl()));
    }

    @Override
    public void removeRecycleBin(RecycleBinRemoveReqDTO requestParam) {
        LambdaUpdateWrapper<ShortLinkDO> eq = Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getEnableStatus, 0)
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl());
                //.eq(ShortLinkDO::getDelTime,null);
        ShortLinkDO shortLinkDO = new ShortLinkDO();  //.setDelTime(System.currentTimeMillis());
        shortLinkDO.setDelFlag(1);
        baseMapper.update(shortLinkDO,eq);

    }

}
